﻿using CallContext.NETStandard;
using System;
using System.Collections.Concurrent;
using System.Threading;

namespace tests
{
    internal class Program
    {
        /// <summary>
        /// 使用concurrent结果：
        /// Factory invoke. got 5
        /// Factory invoke.got 0
        /// Factory invoke. got 1
        /// Factory invoke. got 2
        /// Factory invoke. got 3
        /// Factory invoke. got 4
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            /// 使用LazyConcurrentDictionary结果
            /// Factory invoke.got 0

            var dic = new LazyConcurrentDictionary<int, int>();

            for (int i = 0; i < 6; i++)
            {
                runInNewThread(i);
            }
            void runInNewThread(int i)
            {
                var thread = new Thread(para => dic.GetOrAdd(1, _ => getNum((int)para)));
                thread.Start(i);
            }

            int getNum(int i)
            {
                Console.WriteLine($"Factory invoke. got {i}");
                return i;
            }

            Console.ReadKey();
        }
    }
}
